<template lang="html">
  <div class="container" v-loading="loading">
    <div class="search">
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
              <el-input class="form_input" v-model="searchText" placeholder="请输入订单编号或客户姓名/设计师姓名/店铺名称/产品名称"></el-input>
            </el-form-item>
            <el-form-item label="配送方式">
                <el-select v-model="shippingMethod">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="快递配送" :value="1"></el-option>
                    <el-option label="公司自提" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                <el-button type="primary" :disabled="isShutdownAudit" size="small" @click="onAuidt">一键拆分</el-button>
            </el-form-item>
        </el-form>
    </div>

    <div class="tables">
      <vTable :tableData="tableData" ref="vTable" :date="true" :tInData="tInData" @onCheckAll="onCheckAll" @onCheck="onCheck">
          <template slot="产品" slot-scope="scope">
            <div class="pro">
                <div class="pro_image">
                  <img :src="scope.row.picUrl" alt="">
                </div>
                <div class="pro_info">
                  <div class="pro_name">{{scope.row.commodityName}}</div>
                  <div class="num_color">{{scope.row.materialName}}/{{scope.row.color}}/{{scope.row.laceName}}x{{scope.row.auditNum}}</div>
                  <div class="address">地址：{{scope.row.address}}</div>
                </div>
            </div>
          </template>
          <template slot="客户" slot-scope="scope">
            <div class="userinfo">
              <p>ID：{{scope.row.mbId}}</p>
              <p>姓名：{{scope.row.userName}}</p>
              <p>手机号：{{scope.row.phone}}</p>
            </div>
          </template>
          <template slot="配送方式" slot-scope="scope">
            <p v-if="scope.row.shippingMethod == 1">快递配送</p>
            <p v-if="scope.row.shippingMethod == 2">公司自提</p>
          </template>
          <template slot="售后类型" slot-scope="scope">
            <p v-if="scope.row.afterSalaType == 1">新货调整({{scope.row.isFree == 1?"免费":"收费"}})</p>
            <p v-if="scope.row.afterSalaType == 2">减寸服务({{scope.row.isFree == 1?"免费":"收费"}})</p>
            <p v-if="scope.row.afterSalaType == 3">质量问题({{scope.row.isFree == 1?"免费":"收费"}})</p>
            <p v-if="scope.row.afterSalaType == 4">其他({{scope.row.isFree == 1?"免费":"收费"}})</p>
          </template>
          <template slot="售后原因" slot-scope="scope">
              <div class="remarks">
                  <p>{{scope.row.afterSalaContent}}</p>
                  <el-button type="text" icon="el-icon-edit" @click="handleClick(scope.row)"></el-button>
              </div>
          </template>
          <template slot="状态" slot-scope="scope">
            <div class="">
              <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :inactive-value="2" :active-value="1" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
            </div>
          </template>
          <template slot="操作" slot-scope="scope">
            <div style="padding:0 10px;line-height: 40px;">
              <el-button size="mini" type="primary" @click="handleEdit(scope.row)">拆分</el-button>
              <el-button size="mini" type="danger" @click="handleDetail(scope.row)">详情</el-button>
            </div>
          </template>
      </vTable>

    </div>

    <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />


    <el-dialog title="拆分包裹" append-to-body :visible.sync="dialogVisible" width="30%">
        <div class="dialog">
          <img src="../../assets/img/gantanhao.png" alt="">
          <span>包裹拆分后将回到上一步状态，确认要拆分吗？</span>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data(){
    return {
      loading:false,
      searchText: '',
      shippingMethod: '',
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      dialogVisible: false,
      currentRowData:{},
      tableData: [],
      selectData: [],
      tInData:[
        {
          props: 'orderSublistId',
        },
        {
          props: 'createTime',
        },
        {
          props: 'afterSalaTime',
          name: '售后日期',
        },
        {
          props: 'returnSendTimeAAAA',
        },
        {
          props: 'checkSuccessTimeAAAA',
        },
        {
          title: '产品',
          width: '350px',
        },
        {
          title: '销售价格',
          props: 'commodityPrice',
          width: '120px',
        },
        {
          title: '店铺',
          props: 'branchStoreName',
          width: '200px',
        },
        {
          title: '客户',
          width: '200px',
        },
        {
          title: '设计师',
          props: 'designerName',
          width: '120px',
        },
        {
          title: '工厂名称',
          props: 'factoryName',
          width: '120px',
        },
        {
          title: '应收金额',
          props: 'receivablePrice',
          width: '120px',
        },
        {
          title: '实收金额',
          props: 'receiptsPrice',
          width: '120px',
        },
        {
          title: '售后类型',
          width: '120px',
        },
        {
          title: '售后原因',
          width: '200px',
        },
        // {
        //   title: '结算价格',
        //   props: '',
        //   width: '120px',
        // },
        // {
        //   title: '配送方式',
        //   width: '200px',
        // },
        // {
        //   title: '状态',
        //   width: '120px',
        // },
        {
          title: '操作',
        },
      ],

    }
  },
  computed:{
    address: function() {
      return this.$route.query.address
    },
    isShutdownAudit:function(){
        return this.selectData.length == 0?true:false;
    }
  },
  created(){
    this.selectAfterSalaSendLogPage();
  },
  methods:{
    selectAfterSalaSendLogPage(){
      this.loading = true;
      this.$http.selectAfterSalaSendLogPage({
        address: this.address,
        query: this.searchText,
        shippingMethod: this.shippingMethod,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      }).then(res=>{
        this.tableData = res.data.rows;
        this.tableData.forEach(item=>{
          if(item.afterSalaTime){
            item.afterSalaTime = this.formatTime(item.afterSalaTime);
          }
          if(item.createTime){
            item.createTime = this.formatTime(item.createTime);
          }
        })
        this.pageSize = res.data.pageSize;
        this.totalPage = res.data.total;
        this.loading = false;
      });
    },
    formatTime(value) {
      if(!value) return '';
      let oDate = new Date(value);
      let h = oDate.getHours()<10?'0'+oDate.getHours():oDate.getHours();
      let m = oDate.getMinutes()<10?'0'+oDate.getMinutes():oDate.getMinutes();
      let s = oDate.getSeconds()<10?'0'+oDate.getSeconds():oDate.getSeconds();
      return oDate.getFullYear()+'-'+(oDate.getMonth()+1)+'-'+oDate.getDate()+' '+h+':'+m+':'+s;
    },
    onAuidt(){
      this.currentRowData = null;
      this.dialogVisible = true;
    },
    afterSalaSendLogSplit(data){
      this.$http.afterSalaSendLogSplit(data).then(res=>{
        this.dialogVisible = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.selectAfterSalaSendLogPage();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    afterSalaSendLogSplits(data){
      this.$http.afterSalaSendLogSplits({ids: data}).then(res=>{
        this.dialogVisible = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.selectAfterSalaSendLogPage();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    confirm(){
      if(this.currentRowData){
        let data = {id: this.currentRowData.id};
        this.afterSalaSendLogSplit(data);
      }else{
        let ids = [];
        this.selectData.forEach(item=>{ids.push(item.id);});
        this.afterSalaSendLogSplits(ids.join(','));
      }
    },
    handleEdit(e){
      this.currentRowData = e;
      this.dialogVisible = true;
    },
    handleDetail(e){
      let {href} = this.$router.resolve({ path: '/afterSale/fh_logistics/detail', query:{id: e.id}});
      window.open(href, '_blank');
    },
    onCheckAll(val){
      if(val.status){
        this.selectData = [];
        this.tableData.forEach((item,index)=>{
          this.selectData.push(item);
        })
      }else{
        this.selectData = [];
      }
    },
    onCheck(val){
      if(val.status){
        this.tableData.forEach((item,index)=>{
          if(val.index == index){
            this.selectData.push(item);
          }
        })
      }else{
        this.selectData = this.selectData.filter(arr=>(arr.id!==this.tableData[val.index].id))
      }
    },
    onSwitch(e){
      this.$http.closeAfterSalaSendLog({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.$refs.vTable.onCheckAll(false);
      this.$refs.vTable.checkAll = false;
      this.selectData = [];
      this.currentPage = e;
      this.selectAfterSalaSendLogPage();
    },


  }
}
</script>

<style lang="css" scoped>
</style>
